<html>
<head><meta charset="utf-8"><title>regarding const fns in impl · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html">regarding const fns in impl</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219987906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/219987906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#219987906">(Dec 15 2020 at 14:40)</a>:</h4>
<p>I'm trying to fix <a href="https://github.com/rust-lang/rust/issues/79898">https://github.com/rust-lang/rust/issues/79898</a><br>
This is the code in question</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(const_fn)]</span><span class="w"></span>
<span class="cp">#![feature(const_precise_live_drops)]</span><span class="w"></span>
<span class="cp">#![feature(const_trait_impl)]</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">TestTrait</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">test_fn</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Boo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">t</span>: <span class="nc">T</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">TestTrait</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Boo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">t</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">t</span><span class="p">.</span><span class="n">test_fn</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">forget</span><span class="p">(</span><span class="n">t</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I did some debugging and saw that there is no logic right now to handle this. Nothing's mentioned in the <a href="https://github.com/rust-lang/rfcs/pull/2632">RFC</a> either. So I'm not sure how to fix this.</p>



<a name="219988223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/219988223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#219988223">(Dec 15 2020 at 14:42)</a>:</h4>
<p>The problem I think is that the predicates for <code>T: TestTrait</code> doesn't have the const. If there is a const fn inside the impl, does it mean that the predicates of the bounds have to satisfy consts too?</p>



<a name="219988277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/219988277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#219988277">(Dec 15 2020 at 14:43)</a>:</h4>
<p>CC: <span class="user-mention" data-user-id="211727">@Jonas Schievink</span> because I saw that you implemented this for normal fns</p>



<a name="219988968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/219988968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#219988968">(Dec 15 2020 at 14:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="355545">vn-ki</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/regarding.20const.20fns.20in.20impl/near/219988223">said</a>:</p>
<blockquote>
<p>The problem I think is that the predicates for <code>T: TestTrait</code> doesn't have the const. If there is a const fn inside the impl, does it mean that the predicates of the bounds have to satisfy consts too?</p>
</blockquote>
<p>I think the predicate from the impl should be marked as const while inside the <code>const fn</code>, but not while inside non-const <code>fn</code>s in the same impl.</p>
<p>I don't know how hard it would be to implement this though.</p>



<a name="220013459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/220013459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#220013459">(Dec 15 2020 at 17:31)</a>:</h4>
<p>ah, this is the parent/child bound situation? I though const fn in impl blocks just copied all the bounds from the impl block, but they are just referencing them?</p>



<a name="220013563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/220013563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#220013563">(Dec 15 2020 at 17:31)</a>:</h4>
<p>I guess what we could do is actually copy all bounds for just <code>const fn</code> and make them <code>const Trait</code> bounds</p>



<a name="220103135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/220103135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#220103135">(Dec 16 2020 at 11:15)</a>:</h4>
<p>Copying would probably work, but be unfortunate. Another possibility could be to mutate the bounds as they are extracted. So in the return value of <code>predicates_of</code> we could make the <code>parent</code> field private and add accessor methods that give us the data that consumers of <code>predicates_of</code> need, but only after mutating that data to contain the right bounds</p>



<a name="220105500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/220105500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#220105500">(Dec 16 2020 at 11:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/regarding.20const.20fns.20in.20impl/near/220103135">said</a>:</p>
<blockquote>
<p>Copying would probably work, but be unfortunate. Another possibility could be to mutate the bounds as they are extracted. So in the return value of <code>predicates_of</code> we could make the <code>parent</code> field private and add accessor methods that give us the data that consumers of <code>predicates_of</code> need, but only after mutating that data to contain the right bounds</p>
</blockquote>
<p><a href="https://github.com/vn-ki/rust/blob/e261649593cf9c2707f7b30a61c46c4469c67ebb/compiler/rustc_ty_utils/src/ty.rs#L242-L243"><code>instantiate_into</code> seems to be being used for this in <code>param_env</code></a> (which is the query used in our issue).</p>
<p><a href="https://github.com/vn-ki/rust/blob/e261649593cf9c2707f7b30a61c46c4469c67ebb/compiler/rustc_middle/src/ty/mod.rs#L974-L990">https://github.com/vn-ki/rust/blob/e261649593cf9c2707f7b30a61c46c4469c67ebb/compiler/rustc_middle/src/ty/mod.rs#L974-L990</a></p>



<a name="220105575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/220105575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#220105575">(Dec 16 2020 at 11:45)</a>:</h4>
<p>maybe making constness a field of <code>GenericPredicate</code> works?</p>



<a name="220105658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/220105658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#220105658">(Dec 16 2020 at 11:46)</a>:</h4>
<p>Ah good find</p>



<a name="220105666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/220105666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#220105666">(Dec 16 2020 at 11:46)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/3f2088aa603d2cd3f43c20795872de9cd6ec7735/compiler/rustc_middle/src/ty/mod.rs#L986">https://github.com/rust-lang/rust/blob/3f2088aa603d2cd3f43c20795872de9cd6ec7735/compiler/rustc_middle/src/ty/mod.rs#L986</a></p>



<a name="220105679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/regarding%20const%20fns%20in%20impl/near/220105679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/regarding.20const.20fns.20in.20impl.html#220105679">(Dec 16 2020 at 11:47)</a>:</h4>
<p>here we can mutate the data before putting it into the target</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>